package com.block.risk.service.type.item;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.block.risk.model.result.RiskJudgeResult;
import com.block.risk.service.IDefaultItemService;
import org.springframework.stereotype.Service;

@Service
public class JiaAiNuoItemService implements IDefaultItemService {
    @Override
    public RiskJudgeResult getControlResult(JSONObject resultDetial) {

        /**
         * 一个月有下款  一个月有还款 无逾期无白户600➕就行
         * 近6 个月 还款笔数大于6
         */

        RiskJudgeResult riskJudgeResult = new RiskJudgeResult();
        riskJudgeResult.setPass(false);

        JSONObject currentReportDetail = resultDetial.getJSONObject("current_report_detail");
        JSONObject behaviorReportDetail = resultDetial.getJSONObject("behavior_report_detail");
        JSONObject applyReportDetail = resultDetial.getJSONObject("apply_report_detail");

        if (ObjectUtil.isEmpty(behaviorReportDetail)){
            riskJudgeResult.setResult("行为数据结果为空；【behavior_report_detail】");
            return riskJudgeResult;
        }

        //B22170007 近1个月货款总金额
        String loanAmount = behaviorReportDetail.getString("B22170009");
        if (StrUtil.equals(loanAmount, "0")){
            riskJudgeResult.setResult(StrUtil.format("一个月贷款金额不通过；【{}】", loanAmount));
            return riskJudgeResult;
        }

        // B22170040 近1 个月履约贷款总金额
        String repayAmount = behaviorReportDetail.getString("B22170042");
        if (StrUtil.equals(repayAmount, "0")){
            riskJudgeResult.setResult(StrUtil.format("一个月还款款金额不通过；【{}】", repayAmount));
            return riskJudgeResult;
        }

        // B22170001
        // 贷款行为分 600+
//        String actionSource = behaviorReportDetail.getString("B22170001");
//        if (Integer.valueOf(actionSource) < 600){
//            riskJudgeResult.setResult(StrUtil.format("贷款行为分 600+以上不通过；【{}】", actionSource));
//            return riskJudgeResult;
//        }

        // 近6 个月履约货款次数
        String repayOrderNumber = behaviorReportDetail.getString("B22170047");
        if (Integer.valueOf(repayOrderNumber) < 6){
            riskJudgeResult.setResult(StrUtil.format("近12个月履约贷款次数6笔以上不通过；【{}】", repayOrderNumber));
            return riskJudgeResult;
        }

        // 近6个月逾期M0 = 0
        String numberSixM0 = behaviorReportDetail.getString("B22170025");
        if (Integer.valueOf(numberSixM0) > 0){
            riskJudgeResult.setResult(StrUtil.format("近6个月逾期M0不通过；【{}】", numberSixM0));
            return riskJudgeResult;
        }

        riskJudgeResult.setPass(true);
        riskJudgeResult.setResult("全景分控通过");
        return riskJudgeResult;
    }
}
